package rexsee.core.notification;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.receiver._Receiver;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.text.Html;

public class RexseeNotification implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Notification";
	@Override
	public String getInterfaceName() {
		return (mBrowser == null) ? null : mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeNotification(childBrowser);
	}

	public static final String PREFIX_LAYOUT = "layout_notification_";
	public static final String PREFIX_LAYOUT_TITLE = "notificationTitle_";
	public static final String PREFIX_LAYOUT_MESSAGE = "notificationText_";

	private final Context mContext;
	private final Browser mBrowser;
	public RexseeNotification(Context context) {
		super();
		mContext = context;
		mBrowser = null;
	}
	public RexseeNotification(Browser browser) {
		super();
		mContext = browser.getContext();
		mBrowser = browser;
	}

	public void show(NotificationArgumentsSheet argu) {

		if (argu == null) return;
		if (argu.title.equals("") && argu.message.equals("")) return;

		int id = argu.getId();

		Intent intent;
		if (argu.url.compareTo("") != 0) {
			intent = new Intent(mContext, _Receiver.class);
			intent.setData(Uri.parse(argu.url));
		} else intent = new Intent();
		PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

		Notification notification = new Notification();
		notification.flags = argu.getFlags();
		notification.defaults = argu.getDefaults();
		ApplicationInfo appInfo;
		try {
			appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {
			appInfo = null;
		}
		notification.icon = (appInfo == null) ? android.R.drawable.ic_dialog_info : appInfo.icon;
		notification.tickerText = Html.fromHtml(argu.ticker);
		notification.when = argu.getWhen();

		//notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;
		Uri soundUri = argu.getSound();
		if (soundUri != null) notification.sound = soundUri;

		notification.setLatestEventInfo(mContext, Html.fromHtml(argu.title), Html.fromHtml(argu.message), pendingIntent);
		NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(id, notification);
	}

	public static void show(Context context, int id, int icon, String ticker, String title, String message, String clickUrl, boolean cleanable) {
		show(context, id, icon, ticker, title, message, clickUrl, cleanable, false);
	}
	public static void show(Context context, int id, int icon, String ticker, String title, String message, String clickUrl, boolean cleanable, boolean ringtone) {
		Intent intent;
		if (clickUrl == null) {
			intent = new Intent();
		} else {
			intent = new Intent(context, _Receiver.class);
			intent.setData(Uri.parse(clickUrl));
		}
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		Notification notification = new Notification();
		notification.flags = (cleanable) ? Notification.FLAG_AUTO_CANCEL : Notification.FLAG_NO_CLEAR;
		if (ringtone) {
			notification.defaults = Notification.DEFAULT_SOUND;
		}
		notification.icon = icon;
		notification.tickerText = ticker;
		notification.when = System.currentTimeMillis();
		notification.setLatestEventInfo(context, title, message, pendingIntent);
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(id, notification);
	}
	public static void hide(Context context, int id) {
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(id);
	}

	//JavaScript Interface
	public void show(String arguments) {
		if (arguments == null) return;
		show((new NotificationArgumentsSheet()).parseArguments(arguments));
	}
	public void hide(int id) {
		((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);
	}

}
